package com.zhongyu.controller;

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
import net.sf.jasperreports.export.SimpleXlsxReportConfiguration;
import org.apache.ibatis.io.Resources;
import org.omg.IOP.Encoding;
import org.springframework.boot.autoconfigure.http.HttpProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/sys/yuan")
public class YuanReportController {
    @Resource
    private DataSource dataSource;

    /*
     * 导出Excel
     */
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletRequest request,HttpServletResponse  response)throws Exception{
        System.out.println("dadadada");
        Connection conn= dataSource.getConnection();
//        Map<String,Object> params=new HashMap<>();
//           JasperPrint jasperPrint=JasperFillManager.fillReport("F:/项目/smbms01/src/main/resources/jasper/YuanReport.jasper",params,conn);
           //设置导出时参数设置
//        SimpleXlsxReportConfiguration conf=new SimpleXlsxReportConfiguration();
        InputStream inputStream= Resources.getResourceAsStream("jasper/YuanReport.jasper");

//        JasperReport jasperReport= (JasperReport) JRLoader.loadObject(inputStream);

        JasperPrint jasperPrint= JasperFillManager.fillReport(inputStream,null,conn);
        String tile="YuanReport";
//        response.setCharacterEncoding("UTF-8");
//        Response.ContentEncoding = System.Text.;
//        response.setContentType( "application/vnd.ms-excel;charset=GB2312");
        response.setCharacterEncoding( "GBK");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=gb2312");
        response.setHeader("Content-Disposition","attachment; filename="
                +tile+".xlsx");
        final OutputStream outputStream=response.getOutputStream();
        JRXlsxExporter exporter = new JRXlsxExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
        exporter.exportReport();
//        JasperExportManager.exportReportToPdfStream(jasperPrint,outputStream);
    }

    @GetMapping("/exportPdf")
    public void exportPdf(HttpServletResponse  response)throws Exception{

        InputStream inputStream= Resources.getResourceAsStream("jasper/YuanReport.jasper");
//        Map<String,Object> params=new HashMap<>();
//        JasperReport jasperReport= (JasperReport) JRLoader.loadObject(inputStream);
        Connection conn= dataSource.getConnection();
        JasperPrint jasperPrint= JasperFillManager.fillReport(inputStream,null,conn);
        String tile="YuanReport";
        response.setContentType("application/x-pdf");
        response.setHeader("Content-disposition","inline; filename="+tile+".pdf");
        final OutputStream outputStream=response.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint,outputStream);
    }


}
